package cn.felord.debezium.service;

import cn.felord.debezium.service.impl.TcpActServiceIml;
import lombok.extern.slf4j.Slf4j;
import org.apache.mina.core.session.IoSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static cn.felord.debezium.common.Watch.*;
import static cn.felord.debezium.utils.String2AcsII16.hexToString;

@Service
@Slf4j
public class TcpActService {
    @Autowired
    TcpActServiceIml tcpActServiceIml;

    public void actReceive(String info, IoSession session) {
        String function_code = info.substring(4, 6);//10
        String start_addr = info.substring(6, 10);
        if (info.length() > 16) {
//            log.info("功能码：" + function_code);
//            log.info("起止码：" + start_addr);
            if(info.length() > 20){
                String sub = info.substring(18);//010002004304F904F6FF 设备编码开始 之前去掉
                String type = hexToString(sub.substring(0, 8));//设备编码
                String equipNum = sub.substring(8, 16);//设备编码
                String equipCode = type + equipNum;
//                poolsHash.put(equipCode,session);
            }
        }
        if (info.length() == 16) {     //16位是心跳  3030303030303030 ,注册包也是12  [4B44 5443]【KDTC】KDTC00000001 ,20位是收到的回复
            tcpActServiceIml.str16(info,session);
        } else {
            if (function_code.equals(IO_CONFIRM_CODE)) {     //0000只有发送 收不到
                if (info.length() > 20) {    // 20长度是确认回复: FE 01 10 0000 0003 XXXX FF
                    if (start_addr.equals(START_CODE01)) {
                        tcpActServiceIml.addFeeding(info, session);//添加物料
                    } else if (start_addr.equals(START_CODE02)) {
                        tcpActServiceIml.updateEquipments(info,session);//设置设备
                    } else if (start_addr.equals(START_CODE03)) {
                        tcpActServiceIml.updateEquipmentInfo(info,session);
                    } else if (start_addr.equals(START_CODE04)) {
                        tcpActServiceIml.updateElements(info,session);
                    } else if (start_addr.equals(START_CODE05)) {
                        tcpActServiceIml.updateBeforeMaterias(info,session);
                    } else if (start_addr.equals(START_CODE06)) {
                        tcpActServiceIml.updateAfterQhqDetectionStand(info,session);
                    } else {
                        log.error("未匹配上起始位：" + start_addr);
                    }
                } else {
                    log.info("收到确认消息，不再回复！起始位：" + start_addr);
                }
            } else if (function_code.equals(QUERY_CODE)) {
               if (start_addr.equals(START_CODE02)) { //0001没有查询物料 只有新增物料，其他02开始就有相互了
                   tcpActServiceIml.queryEquipments(info,session);
                } else if (start_addr.equals(START_CODE03)) {
                   tcpActServiceIml.queryEquipmentInfo(info,session);
                } else if (start_addr.equals(START_CODE04)) {
                   tcpActServiceIml.queryElements(info,session);
                } else if (start_addr.equals(START_CODE05)) {
                   tcpActServiceIml.queryBeforeMaterias(info,session);
                } else if (start_addr.equals(START_CODE06)) {
                   tcpActServiceIml.queryAfterQhqDetectionStand(info,session);
                } else {
                    log.error("未匹配上起始位：" + start_addr);
                }
            }
        }
    }

}
